﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Cust;
using Backend.JYD.Model;
using Backend.JYD.Business;

namespace Backend.JYD.BLL.Cust
{
    public class CustBaseBLL
    {
        private static readonly IDAL.Cust.ICustBase idal = Factory.DALFactory.CreateEFDAL<IDAL.Cust.ICustBase>("DAL.CustBaseDAL");

        public List<CustBase> GetCustBaseByCustNos(IList<string> custNos)
        {
            if (custNos == null || custNos.Count <= 0) return null;
            var infos = idal.GetCustBaseByCustNos(custNos);
            return infos== null ? null : infos.ToList();
        }

        public CustBase GetCustBaseInfo(string custNo)
        {
            if (string.IsNullOrWhiteSpace(custNo)) return null;
            return idal.GetCustBaseByCustNo(custNo);
        }

        public PageInfo<CustBase> SearchCustBaseInfo(PageInfo page, string no, string name, string phone, int? chanel, bool isAll)
        {
            if (string.IsNullOrWhiteSpace(no) && string.IsNullOrWhiteSpace(name) && string.IsNullOrWhiteSpace(phone)) return null;
            return idal.SearchCustBaseInfo(page, no, name, phone, chanel, isAll);
        }

        public bool UpdateCustBaseInfo(CustBase info)
        {
            if (string.IsNullOrWhiteSpace(info.CustNo)) return false;

            var success = idal.UpdateCustBaseInfo(info);
            var bll = new CustPhoneBLL();
            success = bll.CheckCustPhone(info.CustNo, info.Phone);
            if (!success)
            {
                bll.AddCustPhone(new CustPhone() { CustNo = info.CustNo, Phone = info.Phone, IsDefault = true });
            }
            return success;
        }
    }
}
